package com.sinosoft.cnmuseum.servlets;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.sinosoft.cnmuseum.dao.MbGyFileDao;
import com.sinosoft.cnmuseum.entity.MbGyFile;
import com.sinosoft.cnmuseum.entity.MbManageAudiofrequency;
import com.sinosoft.cnmuseum.entity.MbManageImage;
import com.sinosoft.cnmuseum.entity.MbManageLiterature;
import com.sinosoft.cnmuseum.entity.MbManageResourceresponsibleperson;
import com.sinosoft.cnmuseum.entity.MbManageThreedimensionalmodel;
import com.sinosoft.cnmuseum.entity.MbManageVideo;
import com.sinosoft.cnmuseum.utils.PageUtil;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.commontools.annotated.TransAnnotation;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class MbManageResourceServlet {
  private static Logger logger = Logger.getLogger(MbManageResourceServlet.class);
  
  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getResourceByUser(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      int type = data.getIntValue("type");
      int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
      int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
      HttpSession session = request.getSession(false);
//      String userid = "5";
      String userid = (String) session.getAttribute(Status.SESSION_USERID);
      MbGyFileDao dao = new MbGyFileDao();
      String ordersql = " order by id desc ";
      String limitsql = " limit ";
      StringBuffer sql = new StringBuffer("select * from " + MbGyFile.MB_GY_FILE_TABLE_NAME);
      StringBuffer countsql =  new StringBuffer("select count(id) as count from " + MbGyFile.MB_GY_FILE_TABLE_NAME);
      sql.append(" where " + MbGyFile.ID + " in (");
      countsql.append(" where " + MbGyFile.ID + " in (");
      sql.append(" select " + MbManageResourceresponsibleperson.FILEZDB + " from " + MbManageResourceresponsibleperson.MB_MANAGE_RESOURCERESPONSIBLEPERSON_TABLE_NAME);
      countsql.append(" select " + MbManageResourceresponsibleperson.FILEZDB + " from " + MbManageResourceresponsibleperson.MB_MANAGE_RESOURCERESPONSIBLEPERSON_TABLE_NAME);
      sql.append(" where " + MbManageResourceresponsibleperson.CONTRIBUTOR + " = '" + userid + "'");
      countsql.append(" where " + MbManageResourceresponsibleperson.CONTRIBUTOR + " = '" + userid + "'");
      if (type != 0) {
        sql.append(" and " + MbManageResourceresponsibleperson.RESOURCETYPE + " = '" + type + "'");
        countsql.append(" and " + MbManageResourceresponsibleperson.RESOURCETYPE + " = '" + type + "'");
      }
      sql.append(")");
      countsql.append(")");
      logger.info("CSQL:" + countsql);
      List<Record> countlist = dao.getBySql(countsql.toString());
      int total = countlist.get(0).getIntValue("count");
      PageUtil page = new PageUtil(pagenumber, pagesize, total);
      limitsql += page.getStart()+","+pagesize;
      List<Record> list = dao.getBySql(sql.append(ordersql).append(limitsql).toString());
      // 组装数据
      result.add(Status.ROWS, list);
      result.add(Status.TOTAL, total);
      result.add(Status.PAGENUMBER, pagenumber);
      result.add(Status.PAGESIZE, pagesize);
      result.add(Status.PAGETOTAL, page.getPagetotal());
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        // 事务回滚
        result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }


  @rquestMethod(MethodNote = ReqMode.All)
  @TransAnnotation(isTrans=true)
  public Record getManageResourceByUser(HttpServletRequest request, HttpServletResponse response, Record data) {
    Record result = new Record();
    try {
      int type = data.getIntValue("type");
      int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
      int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
      HttpSession session = request.getSession(false);
//      String userid = "5";
      String userid = (String) session.getAttribute(Status.SESSION_USERID);
      MbGyFileDao dao = new MbGyFileDao();
      String ordersql = " order by id desc ";
      String limitsql = " limit ";
      String tablename = "";
      switch (type) {
        case 1:
          tablename = MbManageImage.MB_MANAGE_IMAGE_TABLE_NAME;
          break;
        case 2:
          tablename = MbManageAudiofrequency.MB_MANAGE_AUDIOFREQUENCY_TABLE_NAME;
          break;
        case 3:
          tablename = MbManageVideo.MB_MANAGE_VIDEO_TABLE_NAME;
          break;
        case 4:
          tablename = MbManageThreedimensionalmodel.MB_MANAGE_THREEDIMENSIONALMODEL_TABLE_NAME;
          break;
        case 5:
          tablename = MbManageLiterature.MB_MANAGE_LITERATURE_TABLE_NAME;
          break;
        default:
          break;
      }
      StringBuffer sql = new StringBuffer();
      sql.append("select file.*,resource.zdbh,resource.title from ")
         .append(tablename)
         .append(" resource, ")
         .append(MbGyFile.MB_GY_FILE_TABLE_NAME)
         .append(" file where resource.fileid=file.id and ");
      
          
//      StringBuffer sql = new StringBuffer("select * from " + MbGyFile.MB_GY_FILE_TABLE_NAME);
      StringBuffer countsql =  new StringBuffer();

      countsql.append("select count(id) as count from ")
         .append(tablename)
         .append(" resource, ")
         .append(MbGyFile.MB_GY_FILE_TABLE_NAME)
         .append(" file where resource.fileid=file.id and ");
      
      sql.append("  file." + MbGyFile.ID + " in (");
      countsql.append("  file." + MbGyFile.ID + " in (");
      sql.append(" select " + MbManageResourceresponsibleperson.FILEZDB + " from " + MbManageResourceresponsibleperson.MB_MANAGE_RESOURCERESPONSIBLEPERSON_TABLE_NAME);
      countsql.append(" select " + MbManageResourceresponsibleperson.FILEZDB + " from " + MbManageResourceresponsibleperson.MB_MANAGE_RESOURCERESPONSIBLEPERSON_TABLE_NAME);
      sql.append(" where " + MbManageResourceresponsibleperson.CONTRIBUTOR + " = '" + userid + "'");
      countsql.append(" where " + MbManageResourceresponsibleperson.CONTRIBUTOR + " = '" + userid + "'");
      if (type != 0) {
        sql.append(" and " + MbManageResourceresponsibleperson.RESOURCETYPE + " = '" + type + "'");
        countsql.append(" and " + MbManageResourceresponsibleperson.RESOURCETYPE + " = '" + type + "'");
      }
      sql.append(")");
      countsql.append(")");
      logger.info("CSQL:" + countsql);
      List<Record> countlist = dao.getBySql(countsql.toString());
      int total = countlist.get(0).getIntValue("count");
      PageUtil page = new PageUtil(pagenumber, pagesize, total);
      limitsql += page.getStart()+","+pagesize;
      List<Record> list = dao.getBySql(sql.append(ordersql).append(limitsql).toString());
      // 组装数据
      result.add(Status.ROWS, list);
      result.add(Status.TOTAL, total);
      result.add(Status.PAGENUMBER, pagenumber);
      result.add(Status.PAGESIZE, pagesize);
      result.add(Status.PAGETOTAL, page.getPagetotal());
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        // 事务回滚
        result.add(Status.SATUS, Status.FAILED);
    }
    return result;
  }
  
}